﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace ZOIL.Library.Behaviors
{
    [Obsolete("Legacy selection behavior", false)]
    public class ZLandscapeShapeSelectionBehavior : ZLandscapeSelectionBehavior
    {
        #region Private Properties

        private SelectionGeometryAdorner _adorner;
        private Brush _fillBrush = new SolidColorBrush(Color.FromArgb(55, 0, 100, 0));
        private AdornerLayer _rootWindowAdornerLayer;
        private Pen _strokePen = new Pen(Brushes.Black, 1);

        #endregion

        #region Construct

        public ZLandscapeShapeSelectionBehavior(ContentControl targetControl)
            : base(targetControl)
        {
            SelectionStarted += OnSelectionStarted;
            SelectionChanged += OnSelectionChanged;
            SelectionFinished += OnSelectionFinished;
            SelectionCancelled += OnSelectionCancelled;
        }

        #endregion

        #region Events

        public void OnSelectionStarted(object sender, ZLandscapeSelectionEventArgs e)
        {
            var rootWindow = (Window) base.FindVisualTreeRoot(base.TargetControl); // Find Rootwindow to
            _rootWindowAdornerLayer = AdornerLayer.GetAdornerLayer((Visual) rootWindow.Content); // draw selection geometry
            _adorner = new SelectionGeometryAdorner((UIElement) rootWindow.Content, e.SelectionPoint, _fillBrush, _strokePen); // on its adorner layer
            _rootWindowAdornerLayer.Add(_adorner);
        }

        public void OnSelectionChanged(object sender, ZLandscapeSelectionEventArgs e)
        {
            _adorner.UpdateSelectionGeometry(e.SelectionPoint);
        }

        private void OnSelectionCancelled(object sender, ZLandscapeSelectionFinishedEventArgs e)
        {
            _rootWindowAdornerLayer.Remove(_adorner);
        }

        public void OnSelectionFinished(object sender, ZLandscapeSelectionFinishedEventArgs e)
        {
            e.SelectedElements = GetElementsInGeometry(_adorner.SelectionShape, base.TargetControl, typeof (Image));
            e.SelectionGeometry = _adorner.SelectionShape; // Add Selection Geometry to event
            _rootWindowAdornerLayer.Remove(_adorner);
        }

        #endregion

        #region Set Shape-Colors

        /// <summary>
        ///     Applies custom colors to the SelectionShape
        /// </summary>
        /// <param name="fillBrush"></param>
        /// <param name="strokePen"></param>
        public void SetShapeColors(Brush fillBrush, Pen strokePen)
        {
            _fillBrush = fillBrush;
            _strokePen = strokePen;
        }

        #endregion
    }

    #region Adorner Class

    public class SelectionGeometryAdorner : Adorner
    {
        #region Private Properties

        private readonly Brush _fillBrush = new SolidColorBrush(Color.FromArgb(55, 0, 100, 0));
        private readonly PathFigure _selectionPathFigure = new PathFigure();
        private readonly Pen _strokePen = new Pen(Brushes.Black, 1);

        #endregion

        #region Public Properties

        public PathGeometry SelectionShape = new PathGeometry();

        #endregion

        #region Construct

        public SelectionGeometryAdorner(UIElement adornerElement, Point startPoint, Brush fillBrush, Pen strokePen)
            : base(adornerElement)
        {
            _selectionPathFigure.StartPoint = startPoint;
            _selectionPathFigure.IsClosed = true;
            _fillBrush = fillBrush;
            _strokePen = strokePen;
            SelectionShape.Figures.Add(_selectionPathFigure);
            IsHitTestVisible = false;
        }

        #endregion

        #region Update Geometry

        public void UpdateSelectionGeometry(Point p)
        {
            _selectionPathFigure.Segments.Add(new LineSegment(p, true));
            InvalidateVisual(); //Forces Adorner to render again
        }

        #endregion

        #region Render

        protected override void OnRender(DrawingContext drawingContext)
        {
            drawingContext.DrawGeometry(_fillBrush, _strokePen, SelectionShape);
            base.OnRender(drawingContext);
        }

        #endregion
    }

    #endregion
}